package cn.stronglink.assetmanage.business.inventory.view.goods;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

import cn.stronglink.assetmanage.R;
import cn.stronglink.assetmanage.business.inventory.model.InventoryDetailModel;
import cn.stronglink.assetmanage.business.inventory.presenter.InventoryDetailPresenter;
import cn.stronglink.assetmanage.business.inventory.view.detail.InventoryDetailView;
import cn.stronglink.assetmanage.entity.Brand;
import cn.stronglink.assetmanage.entity.DataCenter;
import cn.stronglink.assetmanage.entity.InventoryDetail;
import cn.stronglink.assetmanage.entity.Room;
import cn.stronglink.assetmanage.utils.Constants;
import cn.stronglink.assetmanage.widget.MenuOrderDialog;
import cn.stronglink.assetmanage.widget.ViewPagerFragment;

import static cn.stronglink.assetmanage.R.id.sp_room;

/**
 * 盘点数据
 * Created by yuzhantao on 2018/6/21.
 */

public class GoodsDetailFragment extends ViewPagerFragment implements View.OnClickListener{
    private Spinner sp_inventory_brand_name;
    private TextView tv_inventory_use_department;//使用部门
    private Spinner sp_inventory_location;
    private Spinner sp_inventory_datacenter_name;
    private TextView tv_inventory_manage_branch;//管理部门
    private InventoryDetail inventoryDetail;
    private InventoryDetailModel inventoryDetailModel;
    private InventoryDetailView inventoryDetailView;
    private InventoryDetailPresenter inventoryDetailPresenter;
    private TextView tv_inventory_sn_no, tv_inventory_rfid_no;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_inventory_goods_detail, container, false);
        try {
            initViewAndData(rootView);
        }catch (Exception e){
            Toast.makeText(this.getContext(),e.getMessage(),Toast.LENGTH_SHORT).show();
        }
        return rootView;
    }

    private void initViewAndData(View view) {
        sp_inventory_brand_name = view.findViewById(R.id.sp_inventory_brand_name);
        tv_inventory_use_department = view.findViewById(R.id.tv_inventory_use_department);
        sp_inventory_location = view.findViewById(R.id.sp_inventory_location);
        sp_inventory_datacenter_name = view.findViewById(R.id.sp_inventory_datacenter_name);
        tv_inventory_manage_branch = view.findViewById(R.id.tv_inventory_manage_branch);
        tv_inventory_sn_no = view.findViewById(R.id.tv_inventory_sn_no);
        tv_inventory_rfid_no = view.findViewById(R.id.tv_inventory_rfid_no);
        tv_inventory_sn_no.setOnClickListener(this);
        tv_inventory_rfid_no.setOnClickListener(this);
        tv_inventory_use_department.setOnClickListener(this);
        tv_inventory_manage_branch.setOnClickListener(this);
        sp_inventory_brand_name.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                List<Brand> brandList = inventoryDetailModel.getBrandData();
                inventoryDetail.setBrandName(brandList.get(position).getName());
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        sp_inventory_location.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                List<Room> roomList = inventoryDetailModel.getRoomData();
                inventoryDetail.setRoomName(roomList.get(position).getName());
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        sp_inventory_datacenter_name.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                List<DataCenter> dataCenterList = inventoryDetailModel.getDataCenter();
                inventoryDetail.setDatacenterId_(dataCenterList.get(position).getId_());
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        inventoryDetailModel = new InventoryDetailModel();
        inventoryDetailView = new InventoryDetailView(this, view);
        inventoryDetailPresenter = new InventoryDetailPresenter(inventoryDetailModel,inventoryDetailView);
        Bundle bundle = getArguments();
        if(bundle!=null){
            String assetRfid = bundle.getString(Constants.CHECK_IN_DETAIL_NO);
            inventoryDetailPresenter.showDetailData(assetRfid);
        }else{
            inventoryDetailView.showToast(getString(R.string.get_no_data));
        }

    }
    //todo 点击保存，判断是保存数据，还是自动同步数据。如果是已经盘点过的，则只能查看，不能修改；是未盘点的或者盘盈的可以修改

    @Override
    public void onClick(View v) {
        try {
            switch (v.getId()) {
                case R.id.tv_inventory_sn_no:
                    break;
                case R.id.tv_inventory_rfid_no:
                    break;
                case R.id.tv_inventory_use_department:
                    //显示使用部门

                    MenuOrderDialog.Builder builder = new MenuOrderDialog.Builder(getContext());
                    builder.setDatas(inventoryDetailModel.getDepartment());
                    builder.setTitle("使用部门");
                    MenuOrderDialog menuOrderDialog = builder.create();
                    menuOrderDialog.show();
                    break;
                case R.id.tv_inventory_manage_branch:
                    //使用管理部门
                    break;
            }
        }catch (Exception e){
            Toast.makeText(this.getContext(),e.getMessage(),Toast.LENGTH_SHORT).show();
        }
    }
}
